//
//  ZHAQ_Vc.swift
//  Pods
//
//  Created by eme on 2017/4/6.
//
//
/*
 owner:gyh
 update:2016年11月24日11:24:47
 info: 账户安全
 */
import UIKit
import UtilCore
import BrandonCore

class ZHAQ_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var customTableView: UITableView!
    
    /*----------------------------   自定义属性区域    ----------------------------*/
    var spid = ""
    var shopVm:shopMessageVm?
    var messageModel:ShopMseeageModel?{
        didSet{
            self.customTableView.reloadData()
        }
        
    }
    
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let user = AppData.loginedUser{
            self.spid = user.thrid
        }
        self.shopVm?.getShopMessage.onNext(self.spid)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.setupTableView()
    }
    func setupTableView(){
        self.customTableView.showsVerticalScrollIndicator = false
        customTableView.delegate = self
        customTableView.dataSource = self
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        //-----------------------------------------------------------
        //获取店铺信息
        self.shopVm = shopMessageVm()
        
        self.shopVm?.backMessage.subscribe(onNext:{ result in
            
            self.messageModel = result
            
            }).addDisposableTo(disposeBag)
        
        self.shopVm?.loading.bindTo(self.view.rx_loading).addDisposableTo(disposeBag)
        
        self.shopVm?.error.bindTo(self.view.rx_error).addDisposableTo(disposeBag)
    }
}
extension ZHAQ_Vc:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ZHAQ_TableViewCell
        switch  indexPath.row {
        case 0:
            cell.phoneLabel.isHidden = false
            if let model = self.messageModel{
                 if model.spmobile != ""{
                    let startindex = model.spmobile.index(model.spmobile.startIndex, offsetBy: 3)
                    let endIndex = model.spmobile.index(model.spmobile.startIndex, offsetBy: 7)
                    cell.phoneLabel.text = model.spmobile.substring(to: startindex) + "****" + model.spmobile.substring(from: endIndex)
                }
            }
            
            cell.titleLabel.text = "修改绑定手机"
            cell.tipLabel.text = "若您的手机已丢失或停用，请立即更换手机号码"
        default:
            cell.phoneLabel.isHidden = true
            cell.titleLabel.text = "修改登录密码"
            cell.tipLabel.text = "建议您定期修改登录密码，以保护账户安全"
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.perform(#selector(ZHAQ_Vc.deselect), with: nil, afterDelay: 0.1)
        if let model = self.messageModel{
            switch indexPath.row {
            case 0:
                _ = "changephone".openURL(["changeMessage":"修改绑定手机","phone":model.spmobile])
            default:
                _ = "changephone".openURL(["changeMessage":"修改登录密码","phone":model.spmobile])
            }
        }
    }
    func deselect(){
        self.customTableView.deselectRow(at: self.customTableView.indexPathForSelectedRow!, animated: true)
    }
}
